package by.grsu.verkhovodko.page;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.dao.impl.PassengerDao;
import by.grsu.verkhovodko.entity.Passenger;


public class PassengersListPage extends WebPage {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5932372415637808266L;

	@SuppressWarnings({ "serial", "rawtypes" })
	public PassengersListPage() {
		super();
		
		final PassengerDao passengerDao = new PassengerDao(VerkhovodkoWebApplication.DATA_XML_FOLDER);
		final List<Passenger> passengers = passengerDao.getAll();
		
		add(new DataView<Passenger>("table-rows", new ListDataProvider<Passenger>(passengers)) {
			

			
			@Override
			protected void populateItem(final Item<Passenger> item) {
				final Passenger passenger = item.getModelObject();
				// add data for table cell in row
				//item.add(new Label("id", passenger.getId()));
				item.add(new Label("name", passenger.getLogin()));

				// add button component to open "edit" page
				

				/*item.add(new Link("applicationsLink") {
					@Override
					public void onClick() {
						setResponsePage(new ApplicationListPage(passenger));
					}
				});*/
				item.add(new Link("billsLink") {
					@Override
					public void onClick() {
						setResponsePage(new BillListPage(passenger));
					}
				});

				// add button component to delete Passenger
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						// execute "delete" operation
						passengerDao.delete(passenger.getId());
						// reload page
						setResponsePage(new PassengersListPage());
					}
				});
			}
		});
		add(new Link("mainPage") {
			@Override
			public void onClick() {
				setResponsePage(StartPage.class );
			}
		});
		
	}
}
